package com.sky.controller.courier;

import com.alisms.config.AlismsTemplate;
import com.sky.dto.UserLoginDTO;
import com.sky.pojo.Courier;
import com.sky.result.Result;
import com.sky.service.courier.CourierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Description LoginController
 * @Author lyh
 * @Date 2024-11-02
 */
@RestController
@Api(tags = "骑手端登录")
@RequestMapping("/courier")
public class LoginController {
    @Autowired
    private AlismsTemplate alismsTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CourierService courierService;
    @GetMapping("/validateCodeToPhone")
    @ApiOperation("短信验证码")
    public Result<String> sengMessage(String telephone){
        String code="";
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            code+=random.nextInt(10);
        }
        /*try {
            alismsTemplate.sendMessage(telephone,code);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("短信发送失败");
        }*/
        redisTemplate.opsForValue().set("code:"+telephone,code,300, TimeUnit.SECONDS);
        return Result.success(code);
    }
    @PostMapping("/login")
    @ApiOperation("手机验证码登录")
    public Result<Courier> login(@RequestBody UserLoginDTO dto){
     Courier courier=courierService.login(dto);
     return Result.success(courier);
    }

}
